﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using DMS.DAL;
using DMS.Logic;
namespace WebUI.library
{
    public partial class permission : System.Web.UI.Page
    {
        int documentLibId = 0;
        protected void Page_Load(object sender, EventArgs e)
        {
            if (!string.IsNullOrEmpty(Request.QueryString["id"]))
            {
                documentLibId = int.Parse(Request.QueryString["id"]);
            }
            if (!IsPostBack)
            {
                if (documentLibId > 0)
                {
                    loadGvUser();
                    loaddrpUsers();
                }
            }
        }
        void loaddrpUsers()
        {
            List<User> usr = BL.getInstance().getAllUser();
            DropDownList drp = (DropDownList)gvUser.FooterRow.FindControl("drpUser");
            drp.DataSource = usr;
            drp.DataTextField = "userName";
            drp.DataValueField= "ID";
            drp.DataBind();

        }
        void loadGvUser()
        {
            List<UserPermission> up = BL.getInstance().getUserPermissionsForDocumentLibrary(documentLibId);
            if (up==null)
            {
                up = new List<UserPermission>();
                up.Add(new UserPermission());
            }
            gvUser.DataSource = up;
            gvUser.DataBind();
        }
        void cancelGvUser()
        {
            gvUser.EditIndex = -1;
            gvUser.ShowFooter = true;
            loadGvUser();
        }
        protected void gvUser_RowUpdating(object sender, GridViewUpdateEventArgs e)
        {
            DropDownList drpMask = (DropDownList)gvUser.Rows[e.RowIndex].FindControl("drpMask");
            int uid = int.Parse(gvUser.DataKeys[e.RowIndex].Value.ToString());
            short mask = short.Parse(drpMask.SelectedValue);
            UserPermission up = BL.getInstance().getUserPermission(uid, documentLibId);
            up.PermissionMask = mask;
            BL.getInstance().saveUserPermission(up);
            Response.Redirect(Request.RawUrl);
        }

        protected void gvUser_RowDataBound(object sender, GridViewRowEventArgs e)
        {
            if (e.Row.RowType == DataControlRowType.DataRow && ((e.Row.RowState != (DataControlRowState.Alternate | DataControlRowState.Edit) && (e.Row.RowState != (DataControlRowState.Normal | DataControlRowState.Edit)) && (e.Row.RowState != (DataControlRowState.Alternate | DataControlRowState.Insert) && (e.Row.RowState != (DataControlRowState.Normal | DataControlRowState.Insert))))))
            {
                short mask = short.Parse(DataBinder.Eval(e.Row.DataItem, "PermissionMask").ToString());
                if (mask < 1)
                    e.Row.Visible = false;
                else
                {
                    int usrid = (int)DataBinder.Eval(e.Row.DataItem, "UserID");
                    User usr = BL.getInstance().getUserById(usrid);
                    Label lblusr = (Label)e.Row.FindControl("Label1");
                    Label lblmask = (Label)e.Row.FindControl("Label2");
                    lblusr.Text = usr.Name;
                    if (mask == 1)
                        lblmask.Text = "Permit";
                    else
                        lblmask.Text = "Deny";
                }

            }
            else if (e.Row.RowType == DataControlRowType.DataRow && (e.Row.RowState != (DataControlRowState.Alternate | DataControlRowState.Edit) || (e.Row.RowState != (DataControlRowState.Normal | DataControlRowState.Edit))))
            {
                short mask = short.Parse(DataBinder.Eval(e.Row.DataItem, "PermissionMask").ToString());
                int usrid = (int)DataBinder.Eval(e.Row.DataItem, "UserID");
                User usr = BL.getInstance().getUserById(usrid);
                Label lblusr = (Label)e.Row.FindControl("Label3");
                DropDownList drpmask = (DropDownList)e.Row.FindControl("drpMask");
                drpmask.SelectedValue = mask + "";
                lblusr.Text = usr.Name;
 
            }
        }

        protected void gvUser_RowCommand(object sender, GridViewCommandEventArgs e)
        {
            if (e.CommandName == "add")
            {
                DropDownList drpUsr = (DropDownList)gvUser.FooterRow.FindControl("drpUser");
                DropDownList drpMask = (DropDownList)gvUser.FooterRow.FindControl("drpMask");
                int uid=int.Parse(drpUsr.SelectedValue);
                int mask=int.Parse(drpMask.SelectedValue);
                UserPermission up = UserPermission.CreateUserPermission(documentLibId, uid,(short) mask);
                BL.getInstance().saveUserPermission(up);
                Response.Redirect(Request.RawUrl);
            }
        }

        protected void gvUser_RowDeleting(object sender, GridViewDeleteEventArgs e)
        {
            int userid = int.Parse(gvUser.DataKeys[e.RowIndex].Value.ToString());
            BL.getInstance().deleteUserPermission(userid, documentLibId);
            Response.Redirect(Request.RawUrl);
        }

        protected void gvUser_RowEditing(object sender, GridViewEditEventArgs e)
        {
            gvUser.EditIndex = e.NewEditIndex;
            loadGvUser();
        }

        protected void gvUser_RowCancelingEdit(object sender, GridViewCancelEditEventArgs e)
        {
            cancelGvUser();
        }
    }
}
